package com.tianhui.consignortwofei.mvp.presenter;

import android.content.Context;

import com.fgs.common.CommonResponse;
import com.fgs.common.entity.CommonDataListInfo;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.tianhui.consignortwofei.mvp.contract.OrderListContract;
import com.tianhui.consignortwofei.mvp.model.OrderModel;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;

import java.util.List;

import io.reactivex.Observable;

/**
 * @author fgs
 * @date 2020/4/1
 * @description
 **/
public class OrderListPresenter extends OrderListContract.Presenter {
    private Context mContext;
    private OrderModel mModel;

    public OrderListPresenter(Context context) {
        mContext = context;
        mModel = new OrderModel();
    }

    @Override
    public void requestOrderList(Observable observable, boolean isDialog) {
        mModel.orderListModel(mContext, observable, isDialog, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse<CommonDataListInfo>>() {
            @Override
            public void onNext(CommonResponse<CommonDataListInfo> commonResponse) {
                if (null != getView()) {
                    processOrderListResult(commonResponse);
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                if (null != getView()) {
                    getView().setMsg(e.message);
                    getView().stopRefresh();
                }
            }
        });
    }

//    @Override
//    public void requestSourceList(Map<String, String> map, boolean isDialog) {
//        mModel.sourceListModel(mContext, map, isDialog, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse<OrderData>>() {
//            @Override
//            public void onNext(CommonResponse<OrderData> baseResponse) {
//                if (null != getView()) {
//                    processOrderListResult(baseResponse);
//                }
//            }
//
//            @Override
//            public void onError(ExceptionHandle.ResponseThrowable e) {
//                if (null != getView()) {
//                    getView().setMsg(e.message);
//                    getView().stopRefresh();
//                }
//            }
//        });
//    }

//    @Override
//    public void requestDispatchList(Map<String, String> map, boolean isDialog) {
//        mModel.dispatchListModel(mContext, map, isDialog, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse<OrderData>>() {
//            @Override
//            public void onNext(CommonResponse<OrderData> baseResponse) {
//                if (null != getView()) {
//                    processOrderListResult(baseResponse);
//                }
//            }
//
//            @Override
//            public void onError(ExceptionHandle.ResponseThrowable e) {
//                if (null != getView()) {
//                    getView().setMsg(e.message);
//                    getView().stopRefresh();
//                }
//            }
//        });
//    }

    private void processOrderListResult(CommonResponse<CommonDataListInfo> baseResponse) {
        if (baseResponse.getCode() == 20000) {
            CommonDataListInfo data = baseResponse.getData();
            getView().setPageInfo(data.current, data.pages);
            List<OrderInfo> list = data.records;
            if (list != null && list.size() > 0) {
                getView().showDataList(list);
            } else {
                getView().showEmptyView();
            }
        } else {
            getView().setMsg(baseResponse.getMsg());
            getView().stopRefresh();
        }
    }
}
